//模块配置信息
require.config({
	//baseUrl: "./",
	paths: {
		//工具模块
		'less': 'tools/less.min',
		'jquery': 'tools/jquery.min',
		'bootstrap': 'tools/bootstrap.min',
		'handlebars': 'tools/handlebars',

		//配置模块
		'config': './config',
		"appConfig":"./appConfig",

		'top_bar': 'modules/top_bar',
		'problem': 'modules/problem',
		'ask': 'modules/ask',
		'modal_template': 'modules/modal_template',
		'cookie': 'modules/cookie',
		'check_log': 'modules/check_log',
		'login': 'modules/login',
		'other_log': 'modules/login',
		'reg': 'modules/reg',
		'user': 'modules/user',
		'userProblem':'modules/userProblem'　　
	},
	skim: {  //配置依赖关系与接口
		"bootstrap": {	// bootstrap依赖jquery库
			deps: ['jquery'],
			exports: 'bootstrap'
		},
		"handlebars": {
			exports: 'Handlebars'
		},
		"user": {
			deps: ['modal_template']
		}
	}
});
/************************************header***************************************/
//加载顶栏top_bar模块
require(['top_bar'], function(top_bar){
	top_bar();
});
/************************************content***************************************/
require(['jquery','handlebars','cookie','config','less'], function ($,Handlebars,cookie,config){
	require(['bootstrap']);
	var url=window.location.search;
	var pageBelongToId=url.substring(url.indexOf("=")+1);
	var loginUserId=cookie.getCookie("userId");

	//确定当前页面所属用户与登陆用户的关系
	function getRelationBetweenUser(userId1,userId2) {
		var url=config.api.getRelationBetweenUser[0].replace(/\<\w+1\>/,userId1);
		url=url.replace(/\<\w+2\>/,userId2);
		$.ajax({
			url:url,
			type:config.api.getRelationBetweenUser[1],
			success:function(d){
				console.log(d);
				if(d.relation==1||d.relation==3){
					$("#followThisGuy").attr("id","unfollowThisGuy").text("取消关注");
				}
			}
		});
	}
	function fetchFollowList(){
		$.ajax({
			url:config.api.getStudioFollow[0].replace(/\<\w+\>/,pageBelongToId),
			type:config.api.getStudioFollow[1],
			success:function(d){
				console.log(d);
				handlebarsRender("#followList-template",".followListContainer",d);
				$("#otherInfoLink").attr("href","studio.html?studioId="+pageBelongToId);
				$(".userList").on("click",".userItem",function(){
					var id=$(this).data("userid");
					var utype=$(this).data("utype");
					if(utype==2){
						window.location.href="studio.html?studioId="+id;
					}else{
						window.location.href="otherInfo.html?userId="+id;
					}
					
				});
			}
		})
	}
	// function fetchUserData(){
	// 	$.ajax({
	// 		url:config.api.getUserInfo[0].replace(/\<\w+\>/,pageBelongToId),
	// 		type:config.api.getUserInfo[1],
	// 		success:function(d){
	// 			handlebarsRender("#userInfo-template",".socailInfoContainer",d);
	// 			$("#otherInfoLink").attr("href","otherInfo.html?userId="+d.user.userId);
	// 			$("#fansLink").attr("href","othersFans.html?userId="+d.user.userId);
	// 			$("#followLink").attr("href","othersFollow.html?userId="+d.user.userId);
	// 			$("#followBtnContainer").on("click","#followThisGuy",function() {
	// 				followUser(pageBelongToId);
	// 			});
	// 			$("#followBtnContainer").on("click","#unfollowThisGuy",function() {
	// 				unfollowUser(pageBelongToId);
	// 			});
	// 		},
	// 		error:function(){
	// 			alert("获取数据出错");
	// 		}
	// 	});
	// }
	function cancelFollowUser(idToCancel,callback){
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToCancel,
				follow:0
			},
			success:function(d){
				alert("取消关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("取消关注失败");
			}
		});
	}
	function followUser(idToFollow,callback){
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToFollow,
				follow:1
			},
			success:function(d){
				alert("关注成功");
				if(typeof callback=="function"){
					callback();
				}
			},
			error:function(){
				alert("关注失败");
			}
		});
	}
	function handlebarsRender(tplSelector, destSelector, data) {
		//用jquery获取模板
		var source = $(tplSelector).html();
		//预编译模板
		var template = Handlebars.compile(source);
		//匹配json内容
		var html = template(data);
		//输入模板
		$(destSelector).html(html);
	}
	function followUser (idToFollow) {
		var userId=cookie.getCookie("userId");
		if(!userId){
			alert("请先登录");
			return;
		}
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToFollow,
				follow:1
			},
			success:function(d){
				$("#followThisGuy").attr("id","unfollowThisGuy")
								   .text("取消关注");
			},
			error:function(){
				alert("关注失败");
			}
		});
	}
	function unfollowUser (idToUnFollow) {
		var userId=cookie.getCookie("userId");
		if(!userId){
			alert("请先登录");
			return;
		}
		$.ajax({
			url:config.api.followUser[0].replace(/\<\w+\>/,userId),
			type:config.api.followUser[1],
			data:{
				userId:idToUnFollow,
				follow:0
			},
			success:function(d){
				$("#unfollowThisGuy").attr("id","followThisGuy")
								   .text("关注");
			},
			error:function(){
				alert("关注失败");
			}
		});
	}
	Handlebars.registerHelper("followEachOther",function(relation,options){

		if(relation==3){
			return options.fn(this);
		}else{
			return options.inverse(this);
		}
	});//注册一个判断是否相互关注的helper
	fetchFollowList();
	// fetchUserData();
	// getRelationBetweenUser(loginUserId,pageBelongToId);
});
